package com.test.ws.s1.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ws.s1.bean.Acl_user;
import com.test.ws.s1.bean.Userqt;
import com.test.ws.s1.utils.Res;
import com.test.ws.s1.service.imp.UserqtServiceimp;
import com.test.ws.s1.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.UUID;

@Controller
public class UserqtController {
    @Autowired
    private UserqtServiceimp userqtService;

    /// 查
    @RequestMapping("/userqtCha")
    public String selectUserqt( HttpServletRequest request,
                                @RequestParam(defaultValue = "1") int pageNum,
                                @RequestParam(defaultValue = "3") int pageSize
    ){
        System.out.println(pageNum+"---------------------------------"+pageSize);
        request.removeAttribute("sp");
        String userName = request.getParameter("user_name");
        String userPhone = request.getParameter("user_phone");
        Userqt userqt = new Userqt();
        if (userName!=null){
            userqt.setUser_name(userName);
        }

        if (userPhone!=null && !userPhone.equals("")){
            userqt.setUser_phone(userPhone);
        }


        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize);
        ArrayList<Userqt> userqts = userqtService.selectUserqt(userqt);
        PageInfo pageInfo = new PageInfo(userqts);

        System.out.println(userqts);
        request.setAttribute("userqtCha",userqts);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("sp",userqt);

        return "houtai/qtyu";
    }

    /// 回显
    @RequestMapping("/userqtHuo")
    public String selectUserqtById(Integer user_id,HttpServletRequest request){
        String id = request.getParameter("id");
        Userqt userqt = userqtService.selectUserqtById(Integer.parseInt(id));
        System.out.println(userqt);
        request.setAttribute("user",userqt);
        return "houtai/qtyhXiuGai";
    }


    /// 改
    @RequestMapping("/userqtGai")
    public String updateUserqt(Userqt userqt,HttpServletRequest request){
        int i = userqtService.updateUserqt(userqt);
        System.out.println(i);
        return "redirect:/userqtCha";
    }


    /// 修改并且修改D盘图片
    @RequestMapping("/updateUsers")
    public String updateUsers(MultipartFile image , Userqt user, Model model) throws SQLException, IOException {
        if (image != null && image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            ///新增加的头像路径
            String path = Res.fileDir+uuid+"."+houZhui;
            image.transferTo(new File(path));
            user.setUser_tx(uuid+"."+houZhui);
            /// 获取旧头像
            Userqt select = userqtService.selectUserqtById(user.getUser_id());
            String tx = select.getUser_tx();

            if (tx!=null && !tx.equals(user.getUser_tx())){
                File file = new File(Res.fileDir+tx);
                if (file.exists()){
                    file.delete();
                }
            }
            user.setUser_pass(SecurityUtils.encrypt(user.getUser_pass()));
            int i = userqtService.updateUserqt(user);
            if (i>0){
                return "redirect:/userqtCha";
            }

        }
        return "redirect:/userqtCha";
    }

    /// 增加
    @RequestMapping("/userqtAdd")
    public String insertUserqt(Userqt users,HttpServletRequest request,MultipartFile image) throws IOException {
        /// 1. image.getSize() > 0 检查上传的文件是否非空。
        if (image.getSize()>0){
            /// 2. 使用UUID.randomUUID()生成一个全局唯一标识符，并通过replaceAll("-", "")去掉其中的-符号。
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");

            /// 3. 提取文件后缀名：image.getOriginalFilename().split("\\.")[1]，假设文件名为example.jpg，则提取到后缀jpg。
            String houZhui = image.getOriginalFilename().split("\\.")[1];

            image.transferTo(new File(Res.fileDir + uuid+"."+houZhui));

            /// 4. 将UUID与原始文件名拼接起来，生成新的文件名，并调用users.setTx()将其保存到Users对象中。
            users.setUser_tx(uuid+"."+houZhui);

        }

        /// 5. 调用userSever.insertUsers()方法将Users对象插入到数据库中。
        Userqt userqt1 = userqtService.selectUserqtByName(users);
        if (userqt1!=null){
            return "redirect:/userqtCha";
        }

            users.setUser_pass( SecurityUtils.encrypt(users.getUser_pass()));
            int i = userqtService.insertUserqt(users);
            return "redirect:/userqtCha";

    }

    /// 增加页面
    @RequestMapping("/userqtZeng")
    public String updateZeng(Userqt userqt,HttpServletRequest request){
        return "houtai/qtyuZj";
    }

    /// 登录
    @RequestMapping("/userqtDeng")
    public String selectUserqtByName(Userqt userqt,HttpServletRequest request){
        Userqt userqt1 = userqtService.selectUserqtByName(userqt);
        System.out.println("=======!!!!!!"+userqt1);
        request.getSession().setAttribute("qttx",userqt1);
        if (userqt1!=null){
            boolean verify = SecurityUtils.verify(userqt.getUser_pass(), userqt1.getUser_pass());
            if(verify){
                return "index";
            }

        }
        return "denglu";
    }

    /// 退出登录
    @RequestMapping("/tuiChuDengLu")
    public String tuiChuDengLu(Userqt userqt,HttpServletRequest request){
       request.getSession().removeAttribute("qttx");
            return "index";
    }

    /// 注册
    @RequestMapping("/zcUserqt")
    public String zc(Userqt users,HttpServletRequest request,MultipartFile image) throws IOException {
        System.out.println("============================="+users);
        /// 1. image.getSize() > 0 检查上传的文件是否非空。
        if (image.getSize()>0){
            /// 2. 使用UUID.randomUUID()生成一个全局唯一标识符，并通过replaceAll("-", "")去掉其中的-符号。
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");

            /// 3. 提取文件后缀名：image.getOriginalFilename().split("\\.")[1]，假设文件名为example.jpg，则提取到后缀jpg。
            String houZhui = image.getOriginalFilename().split("\\.")[1];

            image.transferTo(new File(Res.fileDir + uuid+"."+houZhui));

            /// 4. 将UUID与原始文件名拼接起来，生成新的文件名，并调用users.setTx()将其保存到Users对象中。
            users.setUser_tx(uuid+"."+houZhui);

        }
        /// 5. 调用userSever.insertUsers()方法将Users对象插入到数据库中。
        Userqt userqt1 = userqtService.selectUserqtByName(users);
        if (userqt1!=null){
            return "zhuce";
        }

        users.setUser_pass( SecurityUtils.encrypt(users.getUser_pass()));
        int i = userqtService.insertUserqt(users);

        return "index";
    }


    /// 删除
    @RequestMapping("/userqtDel")
    public String deleteUserqt(Integer user_id,HttpServletRequest request){
        String id = request.getParameter("id");
        int i = userqtService.deleteUserqt(Integer.parseInt(id));
        System.out.println(i);
        return "redirect:/userqtCha";
    }


    /// 后台登录
    @RequestMapping("/houtaidenglu")
    public String houtaiDenglu(Acl_user acl_user, HttpServletRequest request){
        acl_user.setUsername(request.getParameter("ue"));
        acl_user.setPassword(request.getParameter("pd"));
        Acl_user userqt1 = userqtService.selectUserqtByName1(acl_user);
        System.out.println("============================="+userqt1);
        request.getSession().setAttribute("userqt11",userqt1);
        if (userqt1!=null){
            return "houtai/indexs";
        }
        return "houtai/login";
    }
    /// 后台注册
    @RequestMapping("/zcUserqt1")
    public String zc(HttpServletRequest request,MultipartFile image) throws IOException {
        Acl_user users = new Acl_user();
        String user = request.getParameter("user");
        String pass = request.getParameter("pass");
        users.setUsername(user);
        users.setPassword(pass);
        Acl_user userqt1 = userqtService.selectUserqtByName1(users);
        if (userqt1!=null){
            return "houtai/register";
        }else{
            int i = userqtService.insertUserqt1(users);
            if (i>0){
                return "houtai/indexs";
            }
            return "redirect:/login";
        }
    }


    /// 个人中心修改
    @RequestMapping("/userqtGaiGeRenZx")
    public String updateUserqtGrZx(Userqt user,HttpServletRequest request , MultipartFile image) throws IOException {
        if (image != null && image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            ///新增加的头像路径
            String path = Res.fileDir+uuid+"."+houZhui;
            image.transferTo(new File(path));
            user.setUser_tx(uuid+"."+houZhui);
            /// 获取旧头像
            Userqt select = userqtService.selectUserqtById(user.getUser_id());
            String tx = select.getUser_tx();

            if (tx!=null && !tx.equals(user.getUser_tx())){
                File file = new File(Res.fileDir+tx);
                if (file.exists()){
                    file.delete();
                }
            }
            user.setUser_pass(SecurityUtils.encrypt(user.getUser_pass()));
            int i = userqtService.updateUserqt(user);
            if (i>0){
                return "gerenzhongxin";
            }

        }
        return "gerenzhongxin";
    }
    /// 注销账号
    @RequestMapping("/zhuxiao")
    public String zhuXiao(Userqt userqt,HttpServletRequest request){
        String id = request.getParameter("usid");
        userqt.setUser_id(Integer.parseInt(id));
        int i = userqtService.deleteUserqtZx(userqt);
        request.getSession().removeAttribute("qttx");
        return "index";
    }



}
